﻿Module main
    Public tray As Windows.Forms.NotifyIcon 'Our handy-dandy NotifyIcon so I can reference it anywhere
    Public pipe As Net.Sockets.Socket   'This is useless right now
    Public needs As New Dictionary(Of Integer, SpecialNeed) 'Useless right now
    Public twine As Threading.Thread    'Useless right now
    Public ChildList As New Collection  'The list of registered children
    Public ProviList As New Collection  'The list of registered providers
    Public ActiveUser As New CareGiver(ACCESS_ADMINISTRATOR)    'Active user!
    Public user_registry As New Dictionary(Of String, String)   'Username / passwords by hash
    Public Const DEBUG_MODE As Boolean = False  'Debug mode is full access test mode
    Public Running As Boolean   'Used to stop twine from being a runaway thread
    Public Const hash_add As String = "id\&~+!@._"  'Store it to save myself the headache
    Sub Think()
        'Server communication in here
        While Running
            'Think eight times a second
            Threading.Thread.Sleep(125)
        End While
    End Sub
    Sub Arrive()
        My.Forms.frmQuickList.People = ChildList
        My.Forms.frmQuickList.Arrived = False
        My.Forms.frmQuickList.Show()
    End Sub
    Sub Leave()
        My.Forms.frmQuickList.People = ChildList
        My.Forms.frmQuickList.Arrived = True
        My.Forms.frmQuickList.Show()
    End Sub
    Sub SignOff()
        Try : Running = False : Catch ex As Exception : End Try
        Try : twine.Abort() : Catch ex As Exception : End Try
        Try : tray.Dispose() : Catch ex As Exception : End Try
        Try : Application.Restart() : Catch ex As Exception : End Try
    End Sub
    Sub ChangePassword()
        My.Forms.frmChangePassword.Show()
    End Sub
    Sub Main()
        'Create think thread
        Running = True
        twine = New Threading.Thread(AddressOf Think)

        twine.Start()

        If Not IO.Directory.Exists("id") Then IO.Directory.CreateDirectory("id")
        If Not IO.Directory.Exists("id\worker") Then IO.Directory.CreateDirectory("id\worker")
        If Not IO.Directory.Exists("id\child") Then IO.Directory.CreateDirectory("id\child")
        If Not IO.Directory.Exists("id\logs") Then IO.Directory.CreateDirectory("id\logs")

        IO.File.SetAttributes("id", IO.FileAttributes.Hidden + IO.FileAttributes.System)

        If Not IO.File.Exists(hash_add) Then
            Dim z As New IO.StreamWriter(hash_add, False, System.Text.Encoding.ASCII)
            z.WriteLine(extend.Base64Sha1("admin") & Chr(0) & extend.Base64Sha1("password"))
            z.Close()

            IO.File.Encrypt(hash_add)
            IO.File.SetAttributes(hash_add, IO.FileAttributes.System + IO.FileAttributes.Compressed + IO.FileAttributes.Hidden)
        End If

        IO.File.SetCreationTime(hash_add, New Date(1776, 7, 4))
        IO.File.SetLastAccessTime(hash_add, New Date(1912, 4, 12))
        IO.File.SetLastWriteTime(hash_add, New Date(1932, 1, 1))
        Try
            Dim z As New IO.StreamReader(hash_add)
            Try
                Dim line() As String

                While Not z.EndOfStream
                    line = z.ReadLine.Split(Chr(0))
                    user_registry(line(0)) = line(1)
                End While
            Catch ex As Exception

            End Try

            z.Close()

        Catch ex As Exception

        End Try

        My.Forms.frmLogin.ShowDialog()

        extend.ReloadAll()

        If ActiveUser.SecurityLevel = ACCESS_GUEST Then
            Dim a As New Form

            a.Text = "Employment Termination"
            a.Icon = Icon.FromHandle(My.Resources.note.GetHicon)
            a.Width = 320
            a.Height = 240
            a.FormBorderStyle = FormBorderStyle.FixedToolWindow
            a.ShowInTaskbar = False

            Dim b As New TextBox

            b.Parent = a
            b.Dock = DockStyle.Fill
            b.Multiline = True
            b.ReadOnly = True
            b.Text = My.Computer.FileSystem.ReadAllText(String.Format("id\logs\{0}_fire.log", ActiveUser.SaveFile.Substring(ActiveUser.SaveFile.LastIndexOf("\") + 1)))
            b.SelectionLength = 0

            Dim c As New Button
            c.Text = "OK"
            c.Parent = a
            c.Dock = DockStyle.Bottom

            AddHandler c.Click, AddressOf SignOff

            a.ShowDialog()
        End If

        If Not DEBUG_MODE Then GoTo womlette

        Randomize()

        Dim FNames() As String = {"Bob", "Bill", "Annie", "Jane", "Karl", "Rebecca"}
        Dim LNames() As String = {"Jones", "Smith", "Braidy", "Queen", "Marx"}

        Dim addr As ContactInformation
        Dim ff As ChildInfo

        For i As Integer = 0 To 25
            ff = New ChildInfo
            addr = New ContactInformation

            addr.Line1 = String.Format("{0} {1}", Arguments(CInt(Rnd() * 1024), FNames(CInt(Rnd() * UBound(FNames)))))
            addr.Line2 = ""
            addr.City = "Harrisburg"
            addr.State = "PA"
            addr.ZipCode = CInt(Rnd() * 999)
            addr.PhoneNumber = String.Format("{0}-{1}-{2}", Arguments(CInt(Rnd() * 999), CInt(Rnd() * 999), CInt(Rnd() * 9999)))

            ff.Age = 2 + CInt(Rnd() * 9)
            ff.FirstName = FNames(CInt(Rnd() * UBound(FNames)))
            ff.LastName = LNames(CInt(Rnd() * UBound(LNames)))
            ff.Gender = Chr(Asc("F") + CInt(Rnd()) * (Asc("M") - Asc("F")))
            ff.ContactInformation = addr
            ff.Arrived = CInt(Rnd()) = 1
            ff.ID = GenerateID()

            ChildList.Add(ff)
        Next

womlette:

        Dim menu As New ContextMenu

        Dim item As MenuItem

        tray = New NotifyIcon
        tray.Icon = Icon.FromHandle(My.Resources.anchor.GetHicon)
        tray.Visible = True
        tray.ContextMenu = menu

        If CheckPermission(ACCESS_CAREGIVER) Then
            item = menu.MenuItems.Add("Child Options")

            item.MenuItems.Add("Search Children", AddressOf My.Forms.frmChildSearch.Show)
            item.MenuItems.Add("Child Arrival", AddressOf Arrive)
            item.MenuItems.Add("Child Departure", AddressOf Leave)

            item = menu.MenuItems.Add("Caregiver Options")
            item.MenuItems.Add("Search Caregivers", AddressOf My.Forms.frmCaregivers.Show)
        End If

        If CheckPermission(ACCESS_SUPERVISOR) OrElse CheckPermission(ACCESS_STRANDED) Then
            item = menu.MenuItems.Add("Supervisor Options")
            item.MenuItems.Add("Register New Child", AddressOf My.Forms.frmAddChild.Show)
        End If

        If CheckPermission(ACCESS_ADMINISTRATOR) Then
            item = menu.MenuItems.Add("Administrator Options")
            item.MenuItems.Add("Hire Caregiver", AddressOf My.Forms.frmHire.Show)
        End If

        menu.MenuItems.Add("Change Password", AddressOf My.Forms.frmChangePassword.Show)
        menu.MenuItems.Add("Sign Off", AddressOf SignOff)

        tray.ShowBalloonTip(1000, "Getting Started", String.Format("Right click me to get started, {0}", ActiveUser.FirstName), ToolTipIcon.Info)

        Application.Run() 'This prevents the application from commiting hairy carry, since modal forms == fail here
    End Sub
End Module
